package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.LmTicketCatalogBig;
import cn.wolfcode.trip.base.service.ILmTicketCatalogBigService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("landmarkTicket")
public class LandmarkTicketController {

    @Autowired
    private ILmTicketCatalogBigService lmTicketCatalogBigService;
    @RequestMapping("list")
    public String list(){
        return "landmarkTicket/list";
    }

    /**
     * 根据parentId返回Json格式字符串地区信息
     * @return
     */
    @RequestMapping("serachLandmarkTicket")
    @ResponseBody
    public Object serachLandmarkTicket(Long parentId){
        List<LmTicketCatalogBig> list =  lmTicketCatalogBigService.selectCatalogByParentId(parentId);
        // treeview 格式的字符串 ---> 存放在Map集合中
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        // 要求格式  {text: xxx,tags:xxx,nodes:xxx}
        for (LmTicketCatalogBig lmTicketCatalogBig : list) {
            mapList.add(lmTicketCatalogBig.toMap());
        }
        return mapList;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(LmTicketCatalogBig lmTicketCatalogBig){
        JSONResult result = new JSONResult();
        try {
            if (lmTicketCatalogBig.getId() != null) {
                lmTicketCatalogBigService.update(lmTicketCatalogBig);
            }else {
                lmTicketCatalogBigService.save(lmTicketCatalogBig);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
